package com.itestor.iptv.modules.common.actions;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.itestor.iptv.global.utils.LoggerUtils;
import com.itestor.iptv.modules.common.service.RegionManageService;
import com.itestor.iptv.modules.common.utils.CommonConstant;
import com.itestor.iptv.modules.common.vo.RegionManageOBJ;

/**
 * 
 * @author Mr.li
 * 
 */
public class RegionManageNaviAction extends DispatchAction {

    /**
     * Logger for this class.
     */
    private static final Logger logger = Logger.getLogger(RegionManageNaviAction.class);
    
    private RegionManageService regionManageService;

    /**
     * 添加区局
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward add(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        List<RegionManageOBJ> regionList = regionManageService.getRegionManageList();        
        request.setAttribute("REGION_LIST", regionList);
        return mapping.findForward(CommonConstant.FORWARD_REGION_ADD);
    }

    /**
     * 修改区局
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward modify(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String id = request.getParameter("id");
        if (StringUtils.isEmpty(id)) {
            return null;
        }
        RegionManageOBJ region = regionManageService.get(Integer.parseInt(id));
        
        if (region == null) {
            return null;
        }
        
        LoggerUtils.logActionInfo("To modify the RegionManage  idRegion " + id + ".",
                logger, request);
        

        request.setAttribute(CommonConstant.REQUEST_ATTR_REGION, region);
        
        return mapping.findForward(CommonConstant.FORWARD_REGION_MODIFY);
    }

    /**
     * 删除区局
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward delete(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        String[] pvids = request.getParameterValues("id");
        if (null == pvids) {
            String pvid = request.getParameter("id");
            if (!StringUtils.isEmpty(pvid)) {
                pvids = new String[1];
                pvids[0] = pvid;
            }
        }
        if (null != pvids && pvids.length > 0) {
            
            
            
            int[] ids = new int[pvids.length];
            for (int i = 0; i < pvids.length; i++) {
                try {
                    ids[i] = Integer.parseInt(pvids[i]);
                } catch (NumberFormatException e) {
                    ids[i] = 0;
                }
            }
            LoggerUtils.logActionInfo("Delete the user - id: "
                    + StringUtils.join(pvids, ",") + ".", logger, request);

            regionManageService.delete(ids);
        }

        return mapping.findForward(CommonConstant.FORWARD_REGION_DELETE);
    }

    public void setRegionManageService(RegionManageService pRegionManageService) {
        regionManageService = pRegionManageService;
    }
    
}
